1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import { error } from '@sveltejs/kit';
import { getById } from '$lib/server/services/projects';
import { loadCurrent, loadCompletions } from '$lib/server/services/routines';
import {
completeRoutineAction,
editRoutineAction,
} from '$lib/server/services/routine-actions';
import { numericId } from '$lib/server/services/validation';
import { computeRoutineState } from '$lib/shared/routine-metrics';
import type { Actions, PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params }) => {
const projectId = numericId(params.projectId);
const routineId = numericId(params.routineId);
const project = await getById(projectId);
if (!project || project.archived) {
throw error(404, 'Projet introuvable');
}
const routine = await loadCurrent(routineId);
if (!routine || routine.projectId !== projectId) {
throw error(404, 'Routine introuvable');
}
const completions = await loadCompletions(routineId);
// An archived routine is frozen at its archive instant: state and chart
// stop there instead of now.
const at = routine.archivedAt ? Date.parse(routine.archivedAt) : Date.now();
return {
project: { id: project.id, name: project.name },
routine,
completions,
state: computeRoutineState(routine, completions, at),
};
};
export const actions: Actions = {
edit: async ({ request, params }) => {
const data = await request.formData();
return editRoutineAction(data, numericId(params.routineId), {
type: 'edit',
});
},
complete: async ({ request, params }) => {
const data = await request.formData();
return completeRoutineAction(data, numericId(params.routineId));
},
};
|